package com.example.commons

import kotlinx.serialization.Contextual

/**
 * 返回结果时的统一格式
 *
 * @param <T> 数据类型
</T> */
@kotlinx.serialization.Serializable
data class Ret<T> (
    /**
     * 数据
     */
     val data: T? = null,

    /**
     * 状态码
     */
     val code: Int = 200,

    /**
     * 是否成功
     */
     val success: Boolean = true,

    /**
     * 展示信息
     */
     val msg: String? = null,

    /**
     * 其他额外的信息
     *
     * extra
     */
     val extras: Map<String, @Contextual Any> = emptyMap()
) {
    companion object {
        fun <T> fail(data: T? = null, code: Int = 200, msg: String? = null, extras: Map<String, Any> = emptyMap()): Ret<T> {
            return Ret(data, code, false, msg, extras)
        }

        fun <T> success(data: T? = null, code: Int = 200, msg: String? = null, extras: Map<String, Any> = emptyMap()): Ret<T> {
            return Ret(data, code, true, msg, extras)
        }
    }
}